package com.member.web.controller.shop;

import com.member.shop.domain.SysBizUserIntegralRecord;
import com.member.shop.service.ISysBizUserIntegralRecordService;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.utils.poi.ExcelUtil;
import com.member.system.cache.CommonCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户积分记录Controller
 *
 * @author thomas
 * @date 2022-07-13
 */
@RestController
@RequestMapping("/shop/user/integralRecord")
public class SysBizUserIntegralRecordController extends BaseController {
    @Autowired
    private ISysBizUserIntegralRecordService sysBizUserIntegralRecordService;

    /**
     * 查询用户积分记录列表
     */
    @PreAuthorize("@ss.hasPermi('user:integralRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizUserIntegralRecord sysBizUserIntegralRecord) {
        startPage();
        List<SysBizUserIntegralRecord> list = sysBizUserIntegralRecordService.selectSysBizUserIntegralRecordList(sysBizUserIntegralRecord);
        return getDataTable(list);
    }

    /**
     * 导出用户积分记录列表
     */
    @PreAuthorize("@ss.hasPermi('user:integralRecord:export')")
    @Log(title = "用户积分记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizUserIntegralRecord sysBizUserIntegralRecord) {
        List<SysBizUserIntegralRecord> list = sysBizUserIntegralRecordService.selectSysBizUserIntegralRecordList(sysBizUserIntegralRecord);
        ExcelUtil<SysBizUserIntegralRecord> util = new ExcelUtil<SysBizUserIntegralRecord>(SysBizUserIntegralRecord.class);

        for (SysBizUserIntegralRecord bizUserIntegralRecord : list) {
            bizUserIntegralRecord.setInsertTime(bizUserIntegralRecord.getCreateTime());
            bizUserIntegralRecord.setRemarkInfo(bizUserIntegralRecord.getRemark());

            String dictDataValue = CommonCache.getDictDataValue("biz_integral_change_type" + bizUserIntegralRecord.getChangeType());

            bizUserIntegralRecord.setChangeTypeName(dictDataValue);
        }
        util.exportExcel(response, list, "用户积分记录数据");
    }

    /**
     * 获取用户积分记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('user:integralRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sysBizUserIntegralRecordService.selectSysBizUserIntegralRecordById(id));
    }

    /**
     * 新增用户积分记录
     */
    @PreAuthorize("@ss.hasPermi('user:integralRecord:add')")
    @Log(title = "用户积分记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizUserIntegralRecord sysBizUserIntegralRecord) {
        return toAjax(sysBizUserIntegralRecordService.insertSysBizUserIntegralRecord(sysBizUserIntegralRecord));
    }

    /**
     * 修改用户积分记录
     */
    @PreAuthorize("@ss.hasPermi('user:integralRecord:edit')")
    @Log(title = "用户积分记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizUserIntegralRecord sysBizUserIntegralRecord) {
        return toAjax(sysBizUserIntegralRecordService.updateSysBizUserIntegralRecord(sysBizUserIntegralRecord));
    }

    /**
     * 删除用户积分记录
     */
    @PreAuthorize("@ss.hasPermi('user:integralRecord:remove')")
    @Log(title = "用户积分记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysBizUserIntegralRecordService.deleteSysBizUserIntegralRecordByIds(ids));
    }
}
